home *** CD-ROM | disk | FTP | other *** search
/ CD ROM Paradise Collection 4 / CD ROM Paradise Collection 4 1995 Nov.iso / program / 4cmp22o.zip / DEMO.4TH < prev    next >
Text File  |  1994-08-13  |  5KB  |  125 lines

  1. \ This simple program illustrates the basics of a Forth program prepared
  2. \ for ForthCMP. Comments can be anything after a backslash,
  3. ( or can be text within parenthesis on a single line )
  4. 0 #IF    Interpreted conditionals allow multiline comments, among other
  5.     things, without having to bother with the comment characters.
  6.  
  7.     Lines can be up to 127 characters long, and spaces and tabs are
  8.     ignored.
  9.  
  10.     To compile this program, the compiler, 4C.COM, must be in the
  11.     execution path, and the files DOSGO.SCR and FORTHLIB.SCR must be
  12.     either in the current directory or in a directory pointed to by
  13.     the environment varible 4LIB. For example, "SET 4LIB=d:\FCLIB"
  14.     will cause the directory d:\fclib to be used for file searches
  15.     if the file is not found in the current directory. These library
  16.     files, as all input files, are in source format. Source files have
  17.     the default extensions 4TH or SCR. Either ASCII text files, such
  18.     as this one, or Forth screen files, such as the library files, can
  19.     be used interchangably. Any "included" screen file is read in via
  20.     an implicit "1 LOAD" command.
  21. #THEN
  22.  
  23. \ \\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\
  24. \                                    \
  25. \  If you like ForthCMP, please register it and get the programmers'    \
  26. \  manual plus a disk (specify size) with the current version and    \
  27. \  additional support code. Both 83 Standard and ANS Forth versions     \
  28. \  are available at the same price -- specify version when ordering     \
  29. \                                     \
  30. \  Send a check or money order for $50.00 US to:            \
  31. \      Tom Almy                                \
  32. \      17830 SW Shasta Trail                        \
  33. \      Tualatin, OR 97072                        \
  34. \                                     \
  35. \ RECEIVE BOTH VERSIONS FOR $75.00!                                     \
  36. \                                                                       \
  37. \ (To get the Software Floating Point files, you must prove ownership   \
  38. \  of an LMI Forth Software Floating Point by enclosing a copy of the   \
  39. \  first page in the manual section for Software Floating Point.)       \
  40. \ \\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\
  41.  
  42. \ I80186    \ Uncomment this if you are using an 80186 or later processor
  43.  
  44. 200 SEPSSEG    \ If you uncomment this line, then the program will have a
  45.         \ separate stack segment, 200 bytes long, to hold the
  46.         \ parameter and return stacks. This directive must come
  47.         \ before the MSDOS or MSDOSEXE directive
  48.  
  49. \ ForthCMP programs must start with one of the following directives:
  50.  
  51. 100 MSDOS \ Specifies to generate a COM file, allowing for a return stack
  52.           \ size of 100 bytes. This generates a "tiny" model program.
  53. \ 1000 100 MSDOSEXE \ Specifies to generate an EXE file, allowing 1000 bytes
  54.         \ for the data segment and 100 bytes for the return stack. This 
  55.         \ generates a small model program.
  56.  
  57. \ A maximal sized program can have 63k of code, 64k of static (dictionary)
  58. \ data, and 64k of stack data. You can request additional memory segments
  59. \ from DOS for large data arrays, for instance. Except when SEPSSEG is
  60. \ specified, the parameter stack moves down into the heap. The heap can
  61. \ be allocated using HERE (or DP) and ALLOT, just like in interpreted 
  62. \ Forth. PAD floats above HERE.
  63.  
  64. \ A DRIVER option allows creating device drivers in ForthCMP.
  65.  
  66. \ There is also an option for creating ROMMABLE code for embedded 
  67. \ processors.
  68.  
  69. \ The programmer's manual describes all the options in detail, however there 
  70. \ are enough example programs that you can probably figure most of it out
  71. \ by example.
  72.  
  73. \ A ForthCMP program always contains a function called MAIN that acts as
  74. \ the main entry point. FORTCOM allows forward referencing, so we can safely
  75. \ put the MAIN function first.
  76.  
  77. 1 1 IN/OUT NEED TwoTimes    \ this tells the compiler that a forward
  78.                 \ referenced function has exactly one argument
  79.                 \ and one result value -- this allows the
  80.                 \ compiler to pass the argument and result
  81.                 \ in registers.
  82.                 \ The default is on the stack
  83.  
  84.  
  85. : MAIN
  86.     ." HELLO NEW USER!" CR CR    \ this should look standard.
  87.     ." Type a number:" #IN
  88.     ." Two times the number is: " TwoTimes . CR
  89.     ." Multiplication table"
  90.     MultTable
  91. ;                    \ returns to DOS at the end. Make
  92.                     \ certain the stack is empty, or
  93.                     \ push a 0 on stack if not sure.
  94.                     \ You can also execute BYE at any
  95.                     \ point to return. You can even
  96.                     \ return error codes, if you wish.
  97.  
  98. 1 1 IN/OUT    \ must match any forward declaration
  99.  
  100. CODE TwoTimes    \ We will use an assembly code routine to do the multiply
  101.     AX AX ADD    \ arg and result use AX
  102.     RET        \ couldn't be easier!
  103. END-CODE
  104.  
  105.  
  106. : MultTable    \ We didn't use IN/OUT here
  107.     11 1 DO
  108.         CR
  109.         11 1 DO
  110.         I J *  5 .R    \ print each entry
  111.     LOOP
  112.     LOOP
  113. ;
  114.  
  115. \ Our program is done. Now we need to include the library of 83 Standard
  116. \ functions that are not intrinsically generated.
  117. \ The generated load map shows these functions, as well as the ones above.
  118. \ You can use DEBUG to examine the generated code
  119.  
  120. INCLUDE FORTHLIB
  121.  
  122. END    \ Required last command -- writes out the compiled program
  123.  
  124. \ Now from the command line, execute "4c demo"
  125.